package com.csw;

import java.awt.*;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2022/11/17 22:30
 */
public class Line {
    //起点坐标
    int x = 380;
    int y = 180;
    //终点坐标
    int endx = 500;
    int endy = 500;
    //设置固定线长
    double length = 100;
    double MINI_length=100;
    double Max_length=750;
    //90°弧长
    double huchang = 0;
    //方向
    int dir = 1;
    //状态0:左右摇摆 1:线伸长抓取 2:收回 3:抓取到返回
    int state;
    //红线接收到主窗口的参数
    GameWin frame;

    Line(GameWin frame) {
        this.frame = frame;
    }


    void paintSelf(Graphics g) {
        logic();
        //状态0:左右摇摆 1:线伸长抓取 2:收回 3:抓取到返回
        switch (state) {
            case 0:
                if (huchang < 0.1) {
                    dir = 1;
                } else if (huchang > 0.9) {
                    dir = -1;
                }
                huchang += 0.005 * dir;
                paintLine(g);
                break;
            case 1:
                if (length < Max_length) {
                    length += 10;
                    paintLine(g);
                } else {
                    state = 2;
                }
                break;
            case 2:
                if (length > MINI_length) {
                    length -= 10;
                    paintLine(g);
                } else {
                    state = 0;
                }
            case 3:
                int m=1;
                if (length > MINI_length) {
                    length -= 10;
                    paintLine(g);
                    for (Object object : this.frame.objectList) {
                        if (object.isMove){
                            m=object.m;
                            object.x = endx - object.width/2;
                            object.y = endy;
                            //还有药水能够使用
                            if (Bg.water_can){
                                //药水正在使用中
                                if (Bg.water_use){
                                    if (object.type==1){
                                        m=1;
                                    }
                                    if (object.type==2){
                                        object.x = -250;
                                        object.y = -250;
                                        object.isMove=false;
                                        //药水不在使用中
                                        Bg.water_use=false;
                                        //返回状态2
                                        state=2;
                                    }
                                }
                            }

                            if (length <= 100) {
                                object.x = -250;
                                object.y = -250;
                                object.isMove=false;
                                Bg.water_use=false;
                                Bg.count+=object.score;
                                state = 0;
                            }

                        }

                    }
                    try {
                        Thread.sleep(m);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            default:
        }

    }

    //判定方法
    void logic() {
        for (Object object : this.frame.objectList) {
            if (endx > object.x && endx < (object.x + object.width)
                    && endy > object.y && (endy < object.y + object.height)) {
                state = 3;
                object.isMove=true;
            }
        }

    }



    private void paintLine(Graphics g) {
        endx = (int) (x + length * Math.cos(huchang * Math.PI));
        endy = (int) (y + length * Math.sin(huchang * Math.PI));
        g.setColor(Color.red);
        //让红线加粗
        g.drawLine(x-1, y+1, endx-1, endy+1);
        g.drawLine(x, y, endx, endy);
        g.drawLine(x+1, y-1, endx+1, endy-1);
        g.drawImage(GameUtils.hook, endx-36, endy-2, null);
    }

    void reGame(){
        //弧长
        huchang=0;
        length=100;
    }
}
